﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Model;
public partial class MenuDetail : System.Web.UI.Page
{
    Model.LMenu menu;
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            menu = MenuManager.GetMenuById(Int32.Parse(Request.Params["id"].ToString()), 0);
        }
        catch
        {
            menu = null;
        }

        if (menu != null)
        {
            if (!IsPostBack)
            {
                menu = MenuManager.GetMenuById(Int32.Parse(Request.Params["id"].ToString()), 0);
                Bind(menu);
            }           
            this.btn_New.Visible = false;
            this.btn_Update.Visible = true;
            this.Title = "菜谱："+menu.Name;
        }
        else
        {
            this.btn_New.Visible = true;
            this.btn_Update.Visible = false;
            this.Title = "添加新菜谱" ;
        }

    }
    private void Bind(Model.LMenu m)
    {
        this.tb_Name.Text = m.Name;
        this.tb_Price.Text = m.Price.ToString("F");
        this.tb_Unit.Text = m.Unit;
        this.tb_Code.Text = m.Code;
        this.ddl_Type.SelectedValue = m.Type.ToString();
        this.e_Trait.Text = m.Trait;
    }
    private void Submit()
    {
        menu.Name = this.tb_Name.Text;
        menu.Code = this.tb_Code.Text;
        menu.Trait = this.e_Trait.Text;
        menu.Unit = this.tb_Unit.Text;
        menu.Price = decimal.Parse(this.tb_Price.Text);
        menu.Type = Int32.Parse(this.ddl_Type.SelectedValue);// new Model.Type(Int32.Parse(this.ddl_Type.SelectedValue));
        menu.Language = Int32.Parse(this.ddl_Language.SelectedValue);
        BLL.MenuManager.Update(menu);
    }
    protected void btn_New_Click(object sender, EventArgs e)
    {
        menu = new Model.LMenu();
        Submit();
        int id = BLL.MenuManager.AddNew(menu);
        Response.Redirect("MenuDetail.aspx?id=" + id.ToString());
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        Submit();
        BLL.MenuManager.Update(menu);
    }
    protected void ddl_Language_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (menu != null)
        {
            menu = MenuManager.GetMenuById(Int32.Parse(Request.Params["id"].ToString()), Int32.Parse(this.ddl_Language.SelectedValue));
            Bind(menu);
        }
    }
}
